#include "head.h"


// 双向链表的创建
F_list *D_List_init()
{
    // 1、创建动态空间
    F_list *list = (F_list *)malloc(sizeof(F_list));
    if (list == NULL)
    {
        ERRLOG("创建动态空间失败");
        return NULL;
    }
    // 2、初始化赋值
    list->next = NULL;
    // 3、返回list
    return list;
}

// 双向链表的插入
void D_List_insert(F_list *list, F_data data, int pos)
{
    // 安全判断
    if (list == NULL)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }

    // 创建数据结点
    F_node *new_node = (F_node *)malloc(sizeof(F_node));
    if (new_node == NULL)
    {
        ERRLOG("创建动态空间失败");
        return;
    }
    // 结点初始化
    new_node->data = data;
    new_node->next = new_node;
    new_node->prev = new_node;

    // 头插
    if (pos == 1 || list->next == NULL)
    {
        // 内部有数据的头插
        if (pos == 1 && list->next != NULL)
        {
            F_node *ptr = list->next;
            ptr->prev->next = new_node;
            new_node->next = ptr;
            new_node->prev = ptr->prev;
            ptr->prev = new_node;
            list->next = new_node;
            D_List_file_print(list); // 写入到文件
            return;
        }
        // 内部无数据的头插
        if (list->next == NULL)
        {
            list->next = new_node;
            D_List_file_print(list); // 写入到文件
            return;
        }
    }

    // 中间插入
    F_node *ptr = list->next;

    for (size_t i = 1; i < pos - 1; i++)
    {
        ptr = ptr->next;
    }

    new_node->next = ptr->next;
    ptr->next->prev = new_node;
    new_node->prev = ptr;
    ptr->next = new_node;

    D_List_file_print(list); // 写入到文件
    return;
}

// 双向链表的删除
void D_List_delete(F_list *list, int pos)
{
    // 安全判断
    if (list == NULL)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }
    if (list->next == NULL)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }

    // 删除
    F_node *ptr_1 = list->next;
    F_node *ptr_2 = ptr_1->next;

    // 头删
    if (pos == 1 || ptr_1->next == list->next)
    {
        // 只有一个结点数据的时候
        if (ptr_1->next == list->next)
        {
            free(list->next);
            list->next = NULL;
            D_List_file_print(list); // 写入到文件
            return;
        }
        // 多个结点数据删第一个
        if (pos == 1)
        {
            list->next = ptr_2;
            ptr_2->prev = ptr_1->prev;
            ptr_1->prev->next = ptr_2;
            free(ptr_1);
            D_List_file_print(list); // 写入到文件
            return;
        }
    }

    // 中间删除
    for (size_t i = 1; i < pos - 1; i++)
    {
        ptr_1 = ptr_1->next;
    }
    ptr_2 = ptr_1->next;
    ptr_1->next = ptr_2->next;
    ptr_2->next->prev = ptr_1;
    free(ptr_2);

    D_List_file_print(list); // 写入到文件
    return;
}

int D_list_up_data_ticket(F_list* list, F_node * node,int num)
{

    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return EOF;
    }
    if (NULL == node)
    {
        ERRLOG("没找到数据");
        return EOF;
    }
    node->data.leftbuyersa+=num;
    return 0;
}




// 双向链表的查找
F_node *D_List_find_id(F_list *list, int id)
{
    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return NULL;
    }
    if (NULL == list->next)
    {
        ERRLOG("链表内无数据");
        return NULL;
    }

    // 查找名字
    if (0== id)
    {
        printf("请输入要查找的名字:");
        scanf("%d" , &id);
    }
    F_node *ptr_node = list->next;
    // 遍历查找
    for (size_t i = 0; NULL != ptr_node; i++)
    {
        if ( ptr_node->data.id ==id )
        {
            return ptr_node;
        }
        ptr_node = ptr_node->next;
    }

    return NULL;
}

F_node *D_List_find_name(F_list *list, char *name)
{
    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return NULL;
    }
    if (NULL == list->next)
    {
        ERRLOG("链表内无数据");
        return NULL;
    }

    // 查找名字
    if (0 == strlen(name))
    {
        printf("请输入要查找的名字:");
        scanf("%s" , name);
    }
    F_node *ptr_node = list->next;
    // 遍历查找
    for (size_t i = 0; NULL != ptr_node; i++)
    {
       
        if (!strcmp( ptr_node->data.flighttype , name ))
        {
            return ptr_node;
        }
        ptr_node = ptr_node->next;
    }

    return NULL;
}



// 双向链表的遍历
void D_List_print(F_list *list)
{
    if (list == NULL)
    {
        fprintf(stderr, "双向循环链表 为 NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        fprintf(stderr, "双向循环链表 无 数据\n");
        return;
    }

    system("clear");

    printf("班次    机型        起点站  终点站  起飞时间    到达时间    飞行时间    票价  载客量  剩余座位  是否飞\n");
    printf("--------------------------------------------------------------------------------------------------\n");

    F_node *ptr = list->next;
    do
    {
        printf("%-6d", ptr->data.id);
        printf("%-15s", ptr->data.flighttype);
        printf("%-10s", ptr->data.startcity);
        printf("%-10s", ptr->data.arrivecity);
        printf("%02d:%02d\t", ptr->data.starttime[0], ptr->data.starttime[1]);
        printf("%02d:%02d\t    ", ptr->data.arrivetime[0], ptr->data.arrivetime[1]);
        printf("%-10s\t", ptr->data.flytime);
        printf("%-6d", ptr->data.value);
        printf("%-8d", ptr->data.maxNUM);
        printf("%-8d  ", ptr->data.leftbuyersa);
        printf("%c\n", ptr->data.whetherfly);
        ptr = ptr->next;
    } while (ptr != list->next);

    char ch = ' ';

    while (ch != 10)
    {
        sleep(1);
        ch = getchar();
    }
}


int D_list_up_data(F_list* list, F_node * node)
{
    if (NULL == list)
    {
        ERRLOG("传入的头结点 为 NULL");
        return EOF;
    }
    if (NULL == node)
    {
        ERRLOG("没找到数据");
        return EOF;
    }

    // 打印原始数据

    printf("ID班次\t机型\t起点站\t终点站\n \
     \t起飞时间\t到达时间\t飞行时间\n \
      \t票价\t载客量\t剩余座位\t是否飞\n");

    printf("%-6d\t%-15s\t%-10s\t%-10s\t\n\
           %02d:%02d\t%02d:%02d\t%-10s\t\n\
           %-6d\t%-8d\t%-8d\t%c\n",\
            node->data.id , node->data.flighttype , node->data.startcity ,node->data.arrivecity,\
            node->data.starttime[0] , node->data.starttime[1],node->data.arrivetime[0] , node->data.arrivetime[1],node->data.flytime,\
            node->data.value,node->data.maxNUM,node->data.leftbuyersa,node->data.whetherfly\
         );

    // 修改
    int num; // 记录用户的输入
 // 开始修改
    while (num != 12)
    {
        printf("1.班次  2.机型  3.出发站  4.到达站  \
5.起飞时间  6.到达时间  7.飞行时间  \
8.票价 9.载客量 10.剩余座位 11.是否飞 12.退出 13.重新输入\n");
        printf("请输入你要修改的选项:\n");
        scanf("%d", &num);
        switch (num)
        {
        case 1:
            printf("请输入班次:");
            scanf("%d", &node->data.id);
            break;
        case 2:
            printf("请输入机型:");
            scanf("%s", node->data.flighttype);
            break;
        case 3:
            printf("请输入出发站:");
            scanf("%s", node->data.startcity);
            break;
        case 4:
            printf("请输入到达站:");
            scanf("%s", node->data.arrivecity);
            break;
        case 5:
            printf("请输入起飞时间:");
            scanf(" %d:%d", &node->data.starttime[0],&node->data.starttime[1]);
            break;
        case 6:
            printf("请输入到达时间:");
            scanf(" %d:%d", &node->data.arrivetime[0],&node->data.arrivetime[1]);
            break;

        case 7:
            printf("请输入飞行时间:");
            scanf("%s", node->data.flytime);
            break;
        case 8:
            printf("请输入票价:");
            scanf("%d", &node->data.value);
            break;

        case 9:
            printf("请输入载客量:");
            scanf("%d", &node->data.maxNUM);
            break;

        case 10:
            printf("请输入剩余座位:");
            scanf("%d", &node->data.leftbuyersa);
            break;

        case 11:
            printf("是否起飞(y/n):");
            scanf(" %c", &node->data.whetherfly);
            break;
         case 12:
            printf("退出");
            break;
        default:
            printf("重新输入");
            break;
        }
    }
    
    return 0;
}





// 双向链表的清空
void D_List_clear(F_list *list)
{
    if (list == NULL)
    {
        ERRLOG("双向循环链表 为 NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        return;
    }

    F_node *ptr_1 = list->next->prev;
    F_node *ptr_2 = list->next->prev->prev;

    while (list->next != ptr_1)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (list->next != ptr_2)
        {
            ptr_2 = ptr_2->prev;
        }
    }
    free(ptr_1);
}

// 双向链表的合并
F_list *D_List_merge(F_list *list1, F_list *list2);

// 双向链表的销毁
void D_List_free(F_list **list)
{
    if (list == NULL || *list == NULL)
    {
        ERRLOG("双向循环链表 为 NULL\n");
        return;
    }
    if ((*list)->next == NULL)
    {
        free(*list);
        *list = NULL;
        return;
    }

    F_node *ptr_1 = (*list)->next->prev;
    F_node *ptr_2 = (*list)->next->prev->prev;

    while ((*list)->next != ptr_1)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if ((*list)->next != ptr_2)
        {
            ptr_2 = ptr_2->prev;
        }
    }
    free(ptr_1);

    free(*list);
    *list = NULL;
}

// 数据操作
// 向文件写入航班信息
void D_List_file_print(F_list *list)
{
    if (list == NULL)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }
    if (list->next == NULL)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }

    FILE *fp = fopen(FLIGHTS_FILE, "w+");
    if (fp == NULL)
    {
        ERRLOG("文件创建失败");
        return;
    }
    fseek(fp, 0, SEEK_SET);

    F_node *ptr = list->next;
    do
    {
        fprintf(fp, "%d\t", ptr->data.id);
        fprintf(fp, "%s\t", ptr->data.flighttype);
        fprintf(fp, "%s\t", ptr->data.startcity);
        fprintf(fp, "%s\t", ptr->data.arrivecity);
        fprintf(fp, "%d:%d\t", ptr->data.starttime[0], ptr->data.starttime[1]);
        fprintf(fp, "%d:%d\t", ptr->data.arrivetime[0], ptr->data.arrivetime[1]);
        fprintf(fp, "%s\t", ptr->data.flytime);
        fprintf(fp, "%d\t", ptr->data.value);
        fprintf(fp, "%d\t", ptr->data.maxNUM);
        fprintf(fp, "%d\t", ptr->data.leftbuyersa);
        fprintf(fp, "%c\n", ptr->data.whetherfly);
        ptr = ptr->next;
    } while (ptr != list->next);

    fflush(fp); // 刷新文件

    fclose(fp);
}

// 从文件读取航班信息
void D_List_file_scanf(F_list *list)
{
    if (list == NULL)
    {
        ERRLOG("双向循环链表 为 NULL");
        return;
    }

    FILE *fp = fopen(FLIGHTS_FILE, "r+");
    if (fp == NULL)
    {
        ERRLOG("文件打开失败");
        return;
    }
    fseek(fp, 0, SEEK_SET);

    D_List_clear(list);

    F_data data;
    char ch = 0;
    int pos = 0;

    while ((ch = fgetc(fp)) != EOF)
    {
        if (ch == '\n')
        {
            pos++;
        }
    }

    fflush(fp); // 刷新文件

    fseek(fp, 0, SEEK_SET);

    for (size_t i = 0; i < pos; i++)
    {
        fscanf(fp, "%d\t", &data.id);
        fscanf(fp, "%s\t", data.flighttype);
        fscanf(fp, "%s\t", data.startcity);
        fscanf(fp, "%s\t", data.arrivecity);
        fscanf(fp, "%d:%d\t", &data.starttime[0], &data.starttime[1]);
        fscanf(fp, "%d:%d\t", &data.arrivetime[0], &data.arrivetime[1]);
        fscanf(fp, "%s\t", data.flytime);
        fscanf(fp, "%d\t", &data.value);
        fscanf(fp, "%d\t", &data.maxNUM);
        fscanf(fp, "%d\t", &data.leftbuyersa);
        fscanf(fp, "%c\n", &data.whetherfly);
        
        D_List_insert(list, data, i + 1);
    }

    fflush(fp); // 刷新文件

    fclose(fp);
}


